package nocuffin.m5;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


/** 
 * Team: No Cuffin'
 * 2/27/2012
 * 
 *
 * This will be our MainUI!!
 * 
 * 
 * @author Robert Allen, Philip Johnson, Steven Moore
 * @version 1.0 2/27/2012
 *
 */ 
public class TaskContents extends Activity{
	Button bCancel, bComplete, bDelete;
	TextView descriptlabel, locationLabel, dateLabel, commentsLabel, typeLabel, completeContents, completeLabel, typeContents, commentsContents, dateContents, locationContents, descriptContents;
	String collected, descriptoftask, dateoftask, locationoftask, detailsoftask, typeoftask, username, completeness;
	@Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.main5);
	     Bundle b = getIntent().getExtras(); 
	 	 descriptoftask = b.getString("nameoftask");
	 	 dateoftask = b.getString("dateoftask");
	 	 locationoftask = b.getString("locationoftask");
	 	 detailsoftask = b.getString("detailsoftask");
	 	 typeoftask = b.getString("typeoftask");
	 	 username = b.getString("username");
	 	 descriptlabel = (TextView) findViewById (R.id.descriptLabel); 
	 	 descriptContents = (TextView) findViewById (R.id.descriptContents); 
	 	 locationLabel = (TextView) findViewById (R.id.locationLabel); 
	 	 locationContents = (TextView) findViewById (R.id.locationContents);	 	
	 	 dateLabel = (TextView) findViewById (R.id.dateLabel); 
	 	 dateContents = (TextView) findViewById (R.id.dateContents);	 	
	 	 commentsLabel = (TextView) findViewById (R.id.commentsLabel); 
	 	 commentsContents = (TextView) findViewById (R.id.commentsContents);	 	
	 	 typeLabel = (TextView) findViewById (R.id.typeLabel); 
	 	 typeContents = (TextView) findViewById (R.id.typeContents); 
	 	 completeLabel = (TextView) findViewById (R.id.completeLabel); 
	 	 completeContents = (TextView) findViewById (R.id.completeContents);
	 	 bComplete  = (Button) findViewById (R.id.completeButton);
	 	 bCancel = (Button) findViewById (R.id.cancelButton) ;
	 
	 	 descriptContents.setText(descriptoftask);
	 	 locationContents.setText(locationoftask);
	 	 dateContents.setText(dateoftask); 
	 	 commentsContents.setText(detailsoftask.substring(0, detailsoftask.length()-26));
	 	 typeContents.setText(typeoftask);
	 	 completeContents.setText(detailsoftask.substring(detailsoftask.length()-26));
	 	 
	 	 

	 	 bCancel.setOnClickListener(new View.OnClickListener() {
	        	/** 
	        	 * 
	        	 * Manipulates the text boxes upon a click of the available button. 
	        	 * 
	        	 * 
	        	 * @param v Default android param
	        	 *
	        	 */ 
	    		public void onClick(View v) 
	    		{
	    			
					try {
						Class ourClass = Class.forName("nocuffin.m5." + "TaskView");
						Intent myIntent = new Intent(v.getContext(), ourClass);
						Bundle d = new Bundle(); 
	    		 	     //This is our username...which we must pass continuously
						String name1 = username;
	    		        d.putString("name", name1);
	    		        //Add the set of extended data to the intent and start it
	    		        myIntent.putExtras(d);
	    		        //Add the set of extended data to the intent and start it
						startActivity(myIntent);
					} 
					catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
	    		}
	 	 });
	
	 	 bComplete.setOnClickListener(new View.OnClickListener() {
	        	/** 
	        	 * 
	        	 * Manipulates the text boxes upon a click of the available button. 
	        	 * 
	        	 * 
	        	 * @param v Default android param
	        	 *
	        	 */ 
	    		public void onClick(View v) 
	    		{
	    			FileInputStream fis = null;
	    			try 
					{
						fis = openFileInput("key");
						byte[] dataArray = new byte[fis.available()];
						while (fis.read(dataArray) != -1)
						{
							collected = new String(dataArray);
						}
					
					} 
					catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 	
					catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finally
					{
						try 
						{
							fis.close();
						} 
						catch (IOException e) 
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
	    		try {
					fis.close();
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				String[] entries = collected.split("\n");
			
				FileOutputStream fos;
				try 
				{
					fos = new FileOutputStream(Environment.getExternalStorageDirectory());
				} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
				try 
				{
					fos = openFileOutput("key", Context.MODE_WORLD_WRITEABLE);
					for (int i = 0; i < entries.length; i++)
					{
						if (entries[i].contains(descriptoftask) && entries[i].contains(dateoftask) && entries[i].contains(locationoftask) && entries[i].contains(detailsoftask) && entries[i].contains(typeoftask) && entries[i].contains(username))
						{
							if (detailsoftask.substring(detailsoftask.length()-26).contains("not"))
							{
								String entry = username + ":" + descriptoftask + ":" + locationoftask + ":" +  dateoftask + ":" + ((detailsoftask.substring(0, detailsoftask.length()-26)) + "This task is now complete!") + ":" + typeoftask + "\n";
				     		 	fos.write(entry.getBytes());
				     		 	completeContents.setText("This task is now complete!");
				     		 	
							}
							else
							{
								String entry = username + ":" + descriptoftask + ":" + locationoftask + ":" +  dateoftask + ":" + ((detailsoftask.substring(0, detailsoftask.length()-26)) + "This task is now complete!") + ":" + typeoftask + "\n";
				     		 	fos.write(entry.getBytes());
				     		 	completeContents.setText("This task is now complete!");
							}
						}
						else
						{
							fos.write((entries[i]+"\n").getBytes());
						}
					}
					fos.flush();
					fos.close();
					
				} 	
				catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//descriptlabel.setText(collected);
				
	 } 			 
	
});
	
	}}// CLOSING THE CLASS

